package com.viviet.mobileservices.dao.impl;
// default package

import java.util.List;

import org.hibernate.LockMode;

import com.viviet.mobileservices.dao.IVMCustomerDAO;
import com.viviet.mobileservices.dao.entity.VMCustomer;
import com.viviet.mobileservices.util.Logit;

/**
 * A data access object (DAO) providing persistence and search support for
 * BoCustomer entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see .VMCustomer
 * @author MyEclipse Persistence Tools
 */

public class VMCustomerDAO extends BaseHelperDAO implements IVMCustomerDAO {
	private static final Logit LOG = Logit.getInstance(VMCustomerDAO.class);
	// property constants
	public static final String LOGIN_ID = "loginId";
	public static final String LOGIN_PASS = "loginPass";
	public static final String FIRST_LOGIN_PASS = "firstLoginPass";
	public static final String CHANGE_PASSWORD_FLAG = "changePasswordFlag";
	public static final String FULL_NAME = "fullName";
	public static final String FIRST_NAME = "firstName";
	public static final String LAST_NAME = "lastName";
	public static final String BIRTHDAY = "birthday";
	public static final String SEX = "sex";
	public static final String MAIL_ADDRESS = "mailAddress";
	public static final String FAX = "fax";
	public static final String ACTIVE_FLG = "activeFlg";

	public void save(VMCustomer transientInstance) {
		LOG.debug("saving VMCustomer instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			LOG.debug("save successful");
		} catch (RuntimeException re) {
			LOG.error("save failed", re);
			throw re;
		}
	}

	public void delete(VMCustomer persistentInstance) {
		LOG.debug("deleting VMCustomer instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			LOG.debug("delete successful");
		} catch (RuntimeException re) {
			LOG.error("delete failed", re);
			throw re;
		}
	}

	public VMCustomer findById(java.lang.String id) {
		LOG.debug("getting VMCustomer instance with id: " + id);
		try {
			VMCustomer instance = (VMCustomer) getHibernateTemplate().get("com.viviet.mobileservices.dao.entity.VMCustomer", id);
			return instance;
		} catch (RuntimeException re) {
			LOG.error("get failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		LOG.debug("finding VMCustomer instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from VMCustomer as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			LOG.error("find by property name failed", re);
			throw re;
		}
	}
	
	@Override
	public VMCustomer findByCustomerId(java.lang.String id) {
		LOG.debug("getting VMCustomer instance with customerId: " + id);
		try {
			String queryString = "from VMCustomer as model where model.customerId= ?";
			List<VMCustomer> list = (List<VMCustomer>)getHibernateTemplate().find(queryString, id);
			return list.isEmpty()? null:list.get(0); 
		} catch (RuntimeException re) {
			LOG.error("get failed", re);
			throw re;
		}
	}
	
	public List findByEmail(String email) {
		LOG.debug("finding BoCustomer instance with email: " + email);
		try {
			String queryString = "from VMCustomer as model where model.mailAddress = ? OR model.mobileMailAddress = ?";
			return getHibernateTemplate().find(queryString, new Object[]{email,email});
		} catch (RuntimeException re) {
			LOG.error("find by email failed", re);
			throw re;
		}
	}

	public VMCustomer findByLoginId(Object loginId) {
		List<VMCustomer> list = findByProperty(LOGIN_ID, loginId);
		return (list.isEmpty())?null:list.get(0);
	}

	public List findByFullName(Object fullName) {
		return findByProperty(FULL_NAME, fullName);
	}

	public List findByFirstName(Object firstName) {
		return findByProperty(FIRST_NAME, firstName);
	}

	public List findByLastName(Object lastName) {
		return findByProperty(LAST_NAME, lastName);
	}

	public List findByBirthday(Object birthday) {
		return findByProperty(BIRTHDAY, birthday);
	}

	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List findByMailAddress(Object mailAddress) {
		return findByProperty(MAIL_ADDRESS, mailAddress);
	}

	public List findByFax(Object fax) {
		return findByProperty(FAX, fax);
	}

	public List findByActiveFlg(Object activeFlg) {
		return findByProperty(ACTIVE_FLG, activeFlg);
	}

	public List findAll() {
		LOG.debug("finding all BoCustomer instances");
		try {
			String queryString = "from BoCustomer";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			LOG.error("find all failed", re);
			throw re;
		}
	}

	public VMCustomer merge(VMCustomer detachedInstance) {
		LOG.debug("merging BoCustomer instance");
		try {
			VMCustomer result = (VMCustomer) getHibernateTemplate().merge(
					detachedInstance);
			LOG.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			LOG.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(VMCustomer instance) {
		LOG.debug("attaching dirty BoCustomer instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			LOG.debug("attach successful");
		} catch (RuntimeException re) {
			LOG.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(VMCustomer instance) {
		LOG.debug("attaching clean BoCustomer instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			LOG.debug("attach successful");
		} catch (RuntimeException re) {
			LOG.error("attach failed", re);
			throw re;
		}
	}
}